import java.awt.GridLayout;
import java.util.ArrayList;

import javax.swing.JFrame;


class Window extends JFrame {
    private static final long serialVersionUID = -2542001418764869760L;
    // 厉害了, 二维ArrayList
    static ArrayList<ArrayList<DataOfSquare>> Grid;
    static final int width = 20;
    static final int height = 20;

    public Window() {

        // Creates the arrayList that'll contain the threads
        Grid = new ArrayList<>();
        ArrayList<DataOfSquare> data;

        // Creates Threads and its data and adds it to the arrayList
        // 每列都是一个ArrayList
        for (int i = 0; i < width; i++) {
            data = new ArrayList<>();
            for (int j = 0; j < height; j++) {
                DataOfSquare c = new DataOfSquare(2);
                data.add(c);
            }
            Grid.add(data);
        }

        // Setting up the layout of the panel
        getContentPane().setLayout(new GridLayout(20, 20, 0, 0));

        // Start & pauses all threads, then adds every square of each thread to the panel
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                getContentPane().add(Grid.get(i).get(j).square);
            }
        }

        // initial position of the snake
        Tuple snakePosition = new Tuple(0, 0);
        // passing this value to the controller
        ThreadsController c = new ThreadsController(snakePosition);
        //Let's start the game now..
        c.start();

        // Links the window to the keyboardlistenner.
        this.addKeyListener(new KeyboardListener());

        //To do : handle multiplayers .. The above works, test it and see what happens

//        Tuple position2 = new Tuple(13,13);
//        ThreadsController c2 = new ThreadsController(position2);
//        c2.start();

    }
}
